﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/05/27
 * 
 * 功能说明：功能模块字段的管理页面
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;
using System.Collections.Generic;
using System.Text;

public partial class Whir_System_Module_CommonModel_FieldManage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        if (!IsPostBack)
        {
            BindData();
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        string sqlStr = "SELECT * FROM Whir_Field WHERE [modelID] = " + NetRequest.GetQueryInt("ModelID") + " ORDER BY [SORT]";
        Whir.BLL.Whir_Field bll = new Whir.BLL.Whir_Field();

        GridView1.DataSource = Whir.SQLServerDAL.DbHelperSQL.Query(sqlStr);
        GridView1.DataBind();
    }


    protected void EBtnSubmit_Click(object sender, EventArgs e)
    {
        Response.Redirect(string.Format("Field.aspx?Action={0}&ModelID={1}&ModelName={2}", "Add", NetRequest.GetQueryInt("ModelID"), Server.UrlEncode(NetRequest.GetQueryString("ModelName"))));
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Contents/ModelManage.aspx");
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int fieldID = Convert.ToInt32(e.CommandArgument);
        Whir.BLL.Whir_Field bll = new Whir.BLL.Whir_Field();
        Whir.Model.Whir_Field model = bll.GetModel(fieldID);

        //删除字段
        if (e.CommandName == "DeleteField")
        {
            if (ContentManage.FieldInModelFormCheck(model.modelID, fieldID))//检查字段是否在表单中存在
            {
                JsHelper.Alert(Page, "该字段在表单中存在！若确定删除此字段，请先删除该功能模块下表单和表单中的数据。");
            }
            else
            {
                //删除类型
                DeleteFieldType(GetFieldType(model.fieldType), fieldID);

                //删除功能模块表的列
                Whir.Model.Whir_Model m = new Whir.Model.Whir_Model();
                Whir.BLL.Whir_Model b = new Whir.BLL.Whir_Model();
                m = b.GetModel(model.modelID);
                Whir.Common.SqlDMOHelper dmo = new Whir.Common.SqlDMOHelper();
                dmo.DeleteColumnInTable(m.tableName, model.fieldName);

                // 删除字段表对应的数据
                bll.Delete(model.fieldID);
                JsHelper.Alert(Page, "删除字段成功！");
            }
        }
        //禁用字段
        if (e.CommandName == "Disabled")
        {
            if (model.isRequired)
                JsHelper.Alert(Page, "该字段为必填字段，不能禁用");
            else
                model.isUse = false;
        }
        //启用字段
        if (e.CommandName == "Enabled")
        {
            model.isUse = true;
        }

        bll.Update(model);//更新字段
        BindData();
    }

    /// <summary>
    /// 根据枚举值取得枚举实例
    /// </summary>
    protected FieldType GetFieldType(int index)
    {
        return (FieldType)index;
    }

    /// <summary>
    /// 根据枚举值取得枚举描述
    /// </summary>
    protected string FieldTypeDesc(object index)
    {
        return FieldCommon.GetFieldTypeDesc((FieldType)index);
    }

    /// <summary>
    /// 删除字段的对应类型表中数据
    /// </summary>
    /// <param name="type">字段类型</param>
    /// <param name="fieldId">字段 ID</param>
    protected void DeleteFieldType(FieldType type, int fieldId)
    {
        Whir.Model.Whir_FieldText fieldTextModel;
        Whir.BLL.Whir_FieldText fieldTextBll;

        Whir.Model.Whir_FieldEditor fieldEditorModel;
        Whir.BLL.Whir_FieldEditor fieldEditorBLL;

        Whir.Model.Whir_FieldOption fieldOptionModel;
        Whir.BLL.Whir_FieldOption fieldOptionBLL;

        Whir.Model.Whir_FieldUpload fieldUploadModel;
        Whir.BLL.Whir_FieldUpload fieldUploadBLL;

        Whir.Model.Whir_FieldDateText fieldDateTextModel;
        Whir.BLL.Whir_FieldDateText fieldDateTextBLL;
        int fieldTextId = 0, fieldDateTextId = 0, fieldEditorId = 0, fieldOptionId = 0, fieldUploadId = 0;
        switch (type)
        {
            case FieldType.None:
            case FieldType.TextType:
                fieldTextModel = new Whir.Model.Whir_FieldText();
                fieldTextBll = new Whir.BLL.Whir_FieldText();
                fieldTextId = int.Parse(fieldTextBll.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldTextBll.Delete(fieldTextId);
                break;
            case FieldType.MultipleTextType:
                fieldTextModel = new Whir.Model.Whir_FieldText();
                fieldTextBll = new Whir.BLL.Whir_FieldText();
                fieldTextId = int.Parse(fieldTextBll.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldTextBll.Delete(fieldTextId);
                break;
            case FieldType.MultipleHtmlTextType:
                fieldEditorModel = new Whir.Model.Whir_FieldEditor();
                fieldEditorBLL = new Whir.BLL.Whir_FieldEditor();
                fieldEditorId = int.Parse(fieldEditorBLL.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldEditorBLL.Delete(fieldEditorId);
                break;
            case FieldType.ListBoxType:
                fieldOptionModel = new Whir.Model.Whir_FieldOption();
                fieldOptionBLL = new Whir.BLL.Whir_FieldOption();
                fieldOptionId = int.Parse(fieldOptionBLL.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldOptionBLL.Delete(fieldOptionId);
                break;
            case FieldType.NumberType:
                fieldTextModel = new Whir.Model.Whir_FieldText();
                fieldTextBll = new Whir.BLL.Whir_FieldText();
                fieldTextId = int.Parse(fieldTextBll.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldTextBll.Delete(fieldTextId);
                break;
            case FieldType.MoneyType:
                fieldTextModel = new Whir.Model.Whir_FieldText();
                fieldTextBll = new Whir.BLL.Whir_FieldText();
                fieldTextId = int.Parse(fieldTextBll.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldTextBll.Delete(fieldTextId);
                break;
            case FieldType.DateTimeType:
                fieldDateTextModel = new Whir.Model.Whir_FieldDateText();
                fieldDateTextBLL = new Whir.BLL.Whir_FieldDateText();
                fieldDateTextId = int.Parse(fieldDateTextBLL.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldDateTextBLL.Delete(fieldDateTextId);
                break;
            case FieldType.LinkType:
                fieldTextModel = new Whir.Model.Whir_FieldText();
                fieldTextBll = new Whir.BLL.Whir_FieldText();
                fieldTextId = int.Parse(fieldTextBll.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldTextBll.Delete(fieldTextId);
                break;
            case FieldType.BoolType:
                fieldOptionModel = new Whir.Model.Whir_FieldOption();
                fieldOptionBLL = new Whir.BLL.Whir_FieldOption();
                fieldOptionId = int.Parse(fieldOptionBLL.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldOptionBLL.Delete(fieldOptionId);
                break;
            case FieldType.PictureType:
                fieldUploadModel = new Whir.Model.Whir_FieldUpload();
                fieldUploadBLL = new Whir.BLL.Whir_FieldUpload();
                fieldUploadId = int.Parse(fieldUploadBLL.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldUploadBLL.Delete(fieldUploadId);
                break;
            case FieldType.FileType:
                fieldUploadModel = new Whir.Model.Whir_FieldUpload();
                fieldUploadBLL = new Whir.BLL.Whir_FieldUpload();
                fieldUploadId = int.Parse(fieldUploadBLL.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldUploadBLL.Delete(fieldUploadId);
                break;
            case FieldType.ColorType:
                fieldTextModel = new Whir.Model.Whir_FieldText();
                fieldTextBll = new Whir.BLL.Whir_FieldText();
                fieldTextId = int.Parse(fieldTextBll.GetList(" FieldID = " + fieldId).Tables[0].Rows[0][0].ToString());
                fieldTextBll.Delete(fieldTextId);
                break;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int fieldID = 0; //字段ID
        int index = 0; //排序号

        Whir.Model.Whir_Field model = new Whir.Model.Whir_Field();
        Whir.BLL.Whir_Field bll = new Whir.BLL.Whir_Field();

        foreach (GridViewRow row in this.GridView1.Rows)
        {
            DropDownList ddlSortIndex = (DropDownList)row.FindControl("ddlSortIndex");
            index = Convert.ToInt32(ddlSortIndex.SelectedValue);
            fieldID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);

            model = bll.GetModel(fieldID);
            model.sort = index;
            bll.Update(model);
        }
        BindData();

        JsHelper.Alert(Page, "保存排序后，需要重新保存表单！");
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        int count = this.GridView1.Rows.Count;
        string[] strArray = new string[count];
        ListItemCollection items = new ListItemCollection();
        for (int i = 1; i <= count; i++)
        {
            ListItem item = new ListItem(i.ToString(), i.ToString());
            items.Add(item);
        }
        foreach (GridViewRow row in this.GridView1.Rows)
        {
            DropDownList list = (DropDownList)row.FindControl("ddlSortIndex");
            list.DataSource = items;
            list.DataBind();
            list.SelectedIndex = row.RowIndex;
            strArray[row.RowIndex] = list.ClientID;
            list.Attributes.Add("onchange", "Sort(this, " + row.RowIndex.ToString() + "," + count.ToString() + ")");
        }
        StringBuilder builder = new StringBuilder();
        builder.Append("<script language=\"JavaScript\" type=\"text/JavaScript\">");
        builder.Append("var arrId = new Array(");
        for (int j = 0; j < count; j++)
        {
            builder.Append("\"" + strArray[j] + "\",");
        }
        builder.Remove(builder.Length - 1, 1);
        builder.Append(");\n");
        builder.Append("function Sort(eSelect, iCurrentField, numSelects)\n");
        builder.Append("{\n");
        builder.Append("    var eForm = eSelect.form;\n");
        builder.Append("    var iNewOrder = eSelect.selectedIndex + 1;\n");
        builder.Append("    var iPrevOrder;\n");
        builder.Append("    var positions = new Array(numSelects);\n");
        builder.Append("    var ix;\n");
        builder.Append("    for (ix = 0; ix < numSelects; ix++)\n");
        builder.Append("    {\n");
        builder.Append("            positions[ix] = 0;\n");
        builder.Append("    }\n");
        builder.Append("    for (ix = 0; ix < numSelects; ix++)\n");
        builder.Append("    {\n");
        builder.Append("            positions[eSelect.form[arrId[ix].toString()].selectedIndex] = 1;\n");
        builder.Append("    }\n");
        builder.Append("    for (ix = 0; ix < numSelects; ix++)\n");
        builder.Append("    {\n");
        builder.Append("            if (positions[ix] == 0)\n");
        builder.Append("            {\n");
        builder.Append("                    iPrevOrder = ix + 1;\n");
        builder.Append("                    break;\n");
        builder.Append("            }\n");
        builder.Append("    }\n");
        builder.Append("    if (iNewOrder != iPrevOrder)\n");
        builder.Append("    {\n");
        builder.Append("            var iInc = iNewOrder > iPrevOrder? -1:1\n");
        builder.Append("            var iMin = Math.min(iNewOrder, iPrevOrder);\n");
        builder.Append("            var iMax = Math.max(iNewOrder, iPrevOrder);\n");
        builder.Append("            for (var iField = 0; iField < numSelects; iField++)\n");
        builder.Append("            {\n");
        builder.Append("                    if (iField != iCurrentField)\n");
        builder.Append("                    {\n");
        builder.Append("                            if (eSelect.form[arrId[iField].toString()].selectedIndex + 1 >= iMin && eSelect.form[arrId[iField].toString()].selectedIndex + 1<= iMax)\n");
        builder.Append("                            {\n");
        builder.Append("                                    eSelect.form[arrId[iField].toString()].selectedIndex += iInc;\n");
        builder.Append("                            }\n");
        builder.Append("                    }\n");
        builder.Append("            }\n");
        builder.Append("    }\n");
        builder.Append("}\n");
        builder.Append("</script>\n");
        this.Page.ClientScript.RegisterClientScriptBlock(base.GetType(), "Order", builder.ToString());
    }
}
